﻿/*===============================================================
 TASK 8:                                                {Methods}
 Write a method that adds two positive integer numbers represented
 as arrays of digits (each array element arr[i] contains a digit;
 the last digit is kept in arr[0]). Each of the numbers that
 will be added could have up to 10 000 digits.
 ===============================================================*/

using System;
using System.Numerics;

class AddsTwoPositiveIntegers
{
    static void Main()
    {
        //int n = 456;
        //int m = 15025;

        int n = 1;
        int m = 99;

        BigInteger[] sum = addTwoNubersAsArrays(n, m);
        PrintArray(sum);
    }

    static void PrintArray(BigInteger[] arr)
    {
        foreach (var element in arr)
        {
            Console.Write(element + " ");
        }
        Console.WriteLine();
    }

    static BigInteger[] AddDigitsToArray(int number)
    {
        string num = number.ToString();
        int lenght = num.Length;
        BigInteger[] arr = new BigInteger[lenght];

        for (int i = 0; i < num.Length; i++)
        {
            arr[i] = (int)num[i] - 48;
        }
        return arr;
    }

    static int ReverseDig(int number)
    {

        int digitForReverse = 0;
        int reversedNumber = 0;

        bool areMoreDigits = true;

        while (areMoreDigits)
        {
            digitForReverse = number % 10;
            reversedNumber = reversedNumber * 10 + digitForReverse;
            if (number < 10)
            {
                areMoreDigits = false;
            }
            number = number / 10;
        }
        //string reversed = reversedNumber.ToString();
        return reversedNumber;
    }

    static BigInteger[] addTwoNubersAsArrays(int first, int second)
    {
        int helper = 0;
        if (first < second)
        {
            helper = first;
            first = second;
            second = helper;
        }
        string longerNumber = first.ToString();
        string shorternumber = second.ToString();

        int firstReversed = ReverseDig(first);
        int secondReversed = ReverseDig(second);

        BigInteger[] firstNumber = AddDigitsToArray(firstReversed);
        BigInteger[] secondNumber = AddDigitsToArray(secondReversed);

        int sumLenght = longerNumber.Length + 1;
        BigInteger[] sum = new BigInteger[sumLenght];
        sum[sumLenght-1] = 0;

        int oneInMind = 0;
        int differance = Math.Abs(firstNumber.Length - secondNumber.Length);

        for (int i = 0; i < longerNumber.Length; i++)
        {
            if (differance > 0 && i >= secondNumber.Length)
            {
                sum[i] = firstNumber[i] + oneInMind;
                
            }
            else
            {
                sum[i] =firstNumber[i] + secondNumber[i]+oneInMind;
            }
            oneInMind = 0;
            if (sum[i] > 9)
            {
                sum[i] = sum[i]%10;
                oneInMind=1;
            }
            if (i==longerNumber.Length-1 && oneInMind==1)
            {
                sum[sumLenght - 1] = oneInMind;
            }
        }
        if (sum[sumLenght - 1] == 0)
        {
            Array.Resize(ref sum, sum.Length - 1);
        }

        Array.Reverse(sum);
        return sum;
    }
}

